<template>
  <div>
    <el-dialog :title="!dataForm.id ? '老花镜新增' : (type == 'update') ? '老花镜修改' : '老花镜详细'" :close-on-click-modal="false"
      :visible.sync="visible" :before-close="dialogCloseBefore" fullscreen center>
      <el-button class="miui-dialog-close-btn" @click="dialogCloseBefore">关闭页面</el-button>
      <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
        <el-divider content-position="left">必填项</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="制造商" prop="supplyName">
              <el-input v-model="dataForm.supplyName" readonly="readonly" :disabled="this.type != 'add'">
                <el-button slot="append" @click="getSupplyList" v-if="this.type == 'add'">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品品种" prop="brandName">
              <el-input v-model="dataForm.brandName" readonly="readonly" :disabled="this.type != 'add'">
                <el-button slot="append" @click="getBrandList" v-if="this.type == 'add'">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="dataForm.name" :disabled="this.type == 'check'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="厂家型号" prop="manufacturerSpec">
              <el-input v-model="dataForm.manufacturerSpec"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="型号" prop="spec">
              <el-input v-model="dataForm.spec" :disabled="this.type != 'add'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="老花镜度数" prop="mirrorBall">
              <el-select v-model="dataForm.mirrorBall" :disabled="this.type != 'add'" clearable>
                <el-option v-for="item in mirrorBallOptions" :value='item.value' :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="厂家色号" prop="manufacturerColorNumber">
              <el-input v-model="dataForm.manufacturerColorNumber" :disabled="this.type != 'add'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位" prop="unitType">
              <el-select v-model="dataForm.unitType" clearable>
                <el-option v-for="item in unitTypeData" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收费项目" prop="payType">
              <el-select v-model="dataForm.payType" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.payType" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="批号管理" prop="batchManagement">
              <el-radio-group v-model="dataForm.batchManagement">
                <el-radio :label="1" border>是</el-radio>
                <el-radio :label="0" border>否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品代码" prop="code">
              <el-input v-model="dataForm.code" readonly="readonly"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品条码" prop="barcode">
              <el-input v-model="dataForm.barcode" readonly="readonly"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left">非必填项</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="镜架尺寸" prop="goodsSize">
              <el-input v-model="dataForm.goodsSize" :disabled="this.type == 'check'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="颜色" prop="chineseColor">
              <el-select v-model="dataForm.chineseColor" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.chineseColorData" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="款式" prop="frameStyle">
              <el-select v-model="dataForm.frameStyle" :disabled="this.type == 'check'" clearable>
                <el-option v-for="item in bizData.frameStyleData" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        	<el-col :span="8">
        		<el-form-item label="生产厂家" prop="manufacturerName">
        		  <el-input v-model="dataForm.manufacturerName">
        				<el-button slot="append" @click="openDialog('manufactor')">选择</el-button>
        			</el-input>
        		</el-form-item>
        	</el-col>
          <el-col :span="8">
          	<el-form-item label="供货商" prop="supplierName">
          	  <el-input v-model="dataForm.supplierName">
          			<el-button slot="append" @click="openDialog('supplier')">选择</el-button>
          		</el-input>
          	</el-form-item>
          </el-col>
          <el-col :span="8">
          	<el-form-item label="注册证号" prop="registerNum">
          	  <el-input v-model="dataForm.registerNum"></el-input>
          	</el-form-item>
          </el-col>
        </el-row>
        <goods-price :list="dataForm.list" :priceGroup="priceGroup"></goods-price>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
      </span>
      <select-supply v-if="selectSupplyVisible" ref="selectSupply" @refreshData="updateFormSupply"></select-supply>
      <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
      <search-manufactor v-if="manufactorVisible" ref="manufactorDialog" @refreshData="getManuctorData"></search-manufactor>
      <search-supplier v-if="supplierVisible" ref="supplierDialog" @refreshData="getSupplierData"></search-supplier>
    </el-dialog>
  </div>
</template>

<script>
  import { remote } from '@/api/admin/dict'
  import { goodsGetObj, goodsAddObj, goodsPutObj } from '@/api/eims/goods'
  import { filterParams } from '@/util/tools'
  import { goodsPriceList } from '@/api/eims/goods-price'
  import SelectSupply from '@/components/business/SearchSupply'
  import SelectBrand from '@/components/business/SearchBrand'
  import GoodsPrice from './price'
  import SearchManufactor from '@/components/business/SearchManufactor'
  import SearchSupplier from '@/components/business/SearchSupplier'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        type: '',
        bizData: {},
        priceGroup: [],
        unitTypeData: [],
        dataForm: {
          id: 0,
          ctgId: '8', //  类别
          ctgSaleId: '10', //  商品销售类别
          name: '', //  商品名称
          code: '8.0.0.00000000.0000', //  商品编码
          barcode: '800000000000000', //  商品条码
          supplyId: '', //  供应商ID
          supplyName: '', //  供应商名称
          supplyCode: '',
          brandId: '', //  商品品种ID
          brandName: '', //  商品名称
          brandCode: '',
          status: '', //  商品状态
          unitType: '', //  计量单位
          spec: '', //  型号
          manufacturerSpec: '', //  厂家型号
          batchManagement: 0, //  批号管理
          mirrorBall: '', //  老花镜度数
          manufacturerColorNumber: '', // 厂家色号
          payType: '', //收费项目
          goodsSize: '', //镜架尺寸
          chineseColor: '', //颜色
          frameStyle: '', //款式
          list: [] //价格组
        },
        dataRule: {
          name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
          supplyName: [{required: true, message: '制造商不能为空', trigger: 'blur'}],
          brandName: [{required: true, message: '商品品种不能为空', trigger: 'blur'}],
          unitType: [{required: true, message: '计量单位不能为空', trigger: 'blur'}],
          spec: [{required: true, message: '商品规格/型号不能为空', trigger: 'blur'}],
          manufacturerSpec: [{required: true, message: '厂家型号不能为空', trigger: 'blur'}],
          mirrorBall: [{required: true, message: '老花镜度数不能为空', trigger: 'blur'}]
        },
        selectSupplyVisible: false,
        selectBrandVisible: false,
        manufactorVisible: false,
        supplierVisible: false,
        mirrorBallOptions: [],
      }
    },
    components: {
      SelectSupply,
      SelectBrand,
      GoodsPrice,
      SearchManufactor,
      SearchSupplier
    },
    methods: {
      init(data) {
        this.bizData = this.$store.getters.bizData
        remote('unit_type').then(result => {
          this.unitTypeData = result.data.data
        })
        this.type = data.type
        this.dataForm.id = data.id || 0
        this.visible = true
        this.priceGroup = []
        this.dataForm.list = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // 获取球镜范围
          remote('mirror_ball_enum').then(result => {
            this.mirrorBallOptions = result.data.data
          })
          // dataForm表单中没有supplyId和brandId属性，无法自动重置，进行手动重置
          this.dataForm.supplyId = ''
          this.dataForm.supplyCode = ''
          this.dataForm.brandId = ''
          this.dataForm.brandCode = ''
          if (this.dataForm.id) {
            this.getGoodsPriceList().then((result) => {
              goodsGetObj(this.dataForm.id).then(response => {
                if(response.data.code == '0'){
                  this.dataForm = response.data.data
                  if(this.dataForm.list.length){
                    this.priceGroup = this.dataForm.list
                  }
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            })
          }else{
            this.getGoodsPriceList().then((result) => {
              this.priceGroup = [...result]
              this.dataForm.list = [...result]
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.list = this.priceGroup
            if (this.dataForm.id) {
              goodsPutObj(filterParams(this.dataForm)).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              this.dataForm.status = '1'
              goodsAddObj(filterParams(this.dataForm)).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      dialogCloseBefore(done) {
        this.visible = false
        this.$emit('refreshDataList')
        // done()
      },
      // 打开弹窗
      openDialog(type){
        if(type == 'manufactor'){
          this.manufactorVisible = true
          this.$nextTick(() => {
            this.$refs.manufactorDialog.init()
          })
        }else if(type == 'supplier'){
          this.supplierVisible = true
          this.$nextTick(() => {
            this.$refs.supplierDialog.init()
          })
        }
      },
      // 获取商品制造商信息
      getSupplyList() {
        let data = { ctgId: 8 }
        this.selectSupplyVisible = true
        this.$nextTick(() => {
          this.$refs.selectSupply.init(data)
        })
      },
      // 更新表单制造商信息
      updateFormSupply(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
        this.dataForm.supplyCode = data.code
      },
      // 获取商品品种信息
      getBrandList() {
        if (!this.dataForm.supplyId) {
          this.$message.warning('请选择制造商')
          return false
        }
        let data = {
          ctgId: 8,
          supplyId: this.dataForm.supplyId
        }
        this.selectBrandVisible = true
        this.$nextTick(() => {
          this.$refs.selectBrand.init(data)
        })
      },
      //  更新表单品种信息
      updateFormBrand(data) {
        this.$set(this.dataForm,'brandId',data.id)
        this.$set(this.dataForm,'brandName',data.name)
        this.$set(this.dataForm,'brandCode',data.code)
        this.$set(this.dataForm,'name',data.name)
        this.$set(this.dataForm,'unitType',data.unitType)
        this.$set(this.dataForm,'manufacturerId',data.manufacturerId)
        this.$set(this.dataForm,'manufacturerName',data.manufacturerName)
        this.$set(this.dataForm,'registerNum',data.registerNum)
      },
      //  查询商品价格组列表
      async getGoodsPriceList() {
        let list = []
        await goodsPriceList().then(response => {
          if(response.data.code == '0'){
            let data = response.data.data
            data.forEach(item => {
              let obj = {}
              obj.priceGroupId = item.id
              obj.priceGroupName = item.groupName
              obj.taxRate = item.taxRate || 13
              list.push(obj)
            })
          }else{
            this.$message.error(response.data.msg)
          }
          // this.priceGroup = [...list]
          // this.dataForm.list = [...list]
        })
        return list
      },
      // 获取生产厂家信息
      getManuctorData(data){
        this.$set(this.dataForm,'manufacturerId',data.id)
        this.$set(this.dataForm,'manufacturerName',data.name)
      },
      // 获取供货商信息
      getSupplierData(data){
        this.$set(this.dataForm,'supplierId',data.id)
        this.$set(this.dataForm,'supplierName',data.name)
        this.dataForm = {...this.dataForm}
      }
    },
    computed: {
      newSupply() {
        return this.dataForm.supplyCode
      },
      newBrand() {
        return this.dataForm.brandCode
      },
      newManufacturerSpec() {
        return this.dataForm.manufacturerSpec
      },
      newSpec() {
        return this.dataForm.spec
      },
      newMirrorBall() {
        return this.dataForm.mirrorBall
      }

    },
    watch: {
      // 制造商
      newSupply(val) {
        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + val).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let specNum = (Array(9).join(0) + this.dataForm.spec).slice(-9);
        let mirrorBallNum = (Array(4).join(0) + this.dataForm.mirrorBall.replace(/[\-\+\.]/g, '')).slice(-4)
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + specNum + '.' + mirrorBallNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + specNum + mirrorBallNum
      },
      // 商品品种
      newBrand(val) {
        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + val).slice(-2);
        let specNum = (Array(9).join(0) + this.dataForm.spec).slice(-9);
        let mirrorBallNum = (Array(4).join(0) + this.dataForm.mirrorBall.replace(/[\-\+\.]/g, '')).slice(-4)
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + specNum + '.' + mirrorBallNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + specNum + mirrorBallNum
      },
      // 厂家型号
      newManufacturerSpec(val) {
        let valData = val ? val.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()+]/g,"") : '0'
        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let specNum = (Array(9).join(0) + valData).slice(-9);
        let mirrorBallNum = (Array(4).join(0) + this.dataForm.mirrorBall.replace(/[\-\+\.]/g, '')).slice(-4)
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + specNum + '.' + mirrorBallNum
        this.dataForm.spec = (valData.length > 9) ? valData.substring(valData.length - 9) : valData
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + specNum + mirrorBallNum
      },
      // 型号
      newSpec(val) {
        let valData = val ? val : '0'
        let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
        let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2);
        let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2);
        let specNum = (Array(9).join(0) + valData).slice(-9);
        let mirrorBallNum = (Array(4).join(0) + this.dataForm.mirrorBall.replace(/[\-\+\.]/g, '')).slice(-4)
        this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + specNum + '.' + mirrorBallNum
        this.dataForm.barcode = ctgNum + supplyNum + brandNum + specNum + mirrorBallNum
      },
      //  老花镜度数
      newMirrorBall(val) {
        if(val) {
          let ctgNum = (this.dataForm.ctgId) ? this.dataForm.ctgId : '0'
          let supplyNum = (Array(2).join(0) + this.dataForm.supplyCode).slice(-2)
          let brandNum = (Array(2).join(0) + this.dataForm.brandCode).slice(-2)
          let specNum = (Array(9).join(0) + this.dataForm.spec).slice(-9)
          let mirrorBallNum = (Array(4).join(0) + val.replace(/[\-\+\.]/g, '')).slice(-4)
          this.dataForm.code = ctgNum + '.' + supplyNum + '.' + brandNum + '.' + specNum + '.' + mirrorBallNum
          this.dataForm.barcode = ctgNum + supplyNum + brandNum + specNum + mirrorBallNum
        }
      }
    }
  }
</script>
